/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package teacher;

import common.MessageModel;
import java.net.Socket;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import javax.swing.DefaultListModel;

/**
 * 数据共享中心
 *
 * @author Eugene-Forest
 */
public class DataCenter {

    private static volatile String auth_code="";
    
    private static volatile int count = 0;

    //套接字列表
    private static final BlockingQueue<Socket> sockets = new LinkedBlockingQueue<>();

    //已读公告套接字列表
//    private static final BlockingQueue<Socket> readed_sockets = new LinkedBlockingQueue<>();

    //成员列表
    private static volatile DefaultListModel names = new DefaultListModel();

    private static final BlockingQueue<MessageModel> messageModels = new LinkedBlockingQueue<>();

    protected static synchronized BlockingQueue<MessageModel> get_messageModels() {
        return messageModels;
    }

    protected static synchronized void add_messageModel(MessageModel messageModel) {
        messageModels.add(messageModel);
    }

    protected static synchronized void count_plus() {
        count++;
    }

    protected static synchronized void count_sub() {
        if (count > 0) {
            count--;
        }
    }
    
    protected static synchronized int get_counts(){
        return count;
    }
    
    protected static synchronized void reset_counts(){
        count=0;
    }
    
    protected static synchronized void set_auth_code(String codeString){
        auth_code=codeString;
    }
    
    protected static synchronized String get_auth_code(){
        return auth_code;
    }

//    protected static final ConcurrentMap<String,Socket> socket_map=new ConcurrentHashMap<>();
    protected static synchronized void add_socket(Socket socket) {
        names.addElement("Student-" + socket.getLocalAddress().toString() + "_" + socket.getPort());
        sockets.add(socket);
    }

    protected static synchronized void remove_socket(Socket socket) {
        names.removeElement("Student-" + socket.getLocalAddress().toString() + "_" + socket.getPort());
        sockets.remove(socket);
    }

    protected static synchronized BlockingQueue<Socket> get_sockets(){
        return sockets;
    }
    
    protected static synchronized int get_sockets_size(){
        return sockets.size();
    }

//    protected static synchronized void add_readed_socket(Socket socket) {
//        count_plus();
//        readed_sockets.add(socket);
//    }

//    protected static synchronized void remove_readed_socket(Socket socket) {
//        count_sub();
//        readed_sockets.remove(socket);
//    }

    protected static synchronized DefaultListModel get_names() {
        return names;
    }

    protected static synchronized void refresh_all_names() {
        names.clear();
        for (Socket socket : sockets) {
            names.addElement("Student-" + socket.getLocalAddress().toString() + "_" + socket.getPort());
        }
    }

    protected static synchronized int get_names_size() {
        return names.size();
    }

}
